package com.knowledge.graph.controller;

import com.knowledge.graph.model.entity.User;
import com.knowledge.graph.model.vo.StatusCode;
import com.knowledge.graph.service.CourseService;
import com.knowledge.graph.service.GradeService;
import com.knowledge.graph.model.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author jiwenlong
 * @version 1.0
 * @description: 有关班级请求的处理器，创建班级，删除班级，修改班级，查询班级
 * @date 2024/1/4 17:36
 */
@RestController
@CrossOrigin
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @Resource
    private CourseService courseService;

    @GetMapping("/grade")
    public ResponseVO getGradeByTeacherID(@RequestParam("teacherID") Integer teacherID) {
        return gradeService.queryGrade(teacherID);
    }

    @PostMapping("/grade")
    public ResponseVO postGrade(@RequestParam("teacherID") Integer teacherID, @RequestParam("name") String name, @RequestParam("description") String description) {
        return gradeService.createGrade(name, description, teacherID);
    }

    @PutMapping("/grade")
    public ResponseVO putGrade(@RequestParam("gradeID") Integer gradeID, @RequestParam("name") String name, @RequestParam("description") String description) {
        return gradeService.modifyGrade(gradeID, name, description);
    }

    @DeleteMapping("/grade")
    public ResponseVO deleteGrade(@RequestParam("gradeID") Integer gradeID) {
        return gradeService.removeGrade(gradeID);
    }

    @PutMapping("/grade/importCourse")
    public ResponseVO putGradeOfCourse(@RequestParam("gradeID") Integer gradeID, @RequestParam("courseID") Integer courseID) {
        return gradeService.modifyGradeOfCourse(gradeID, courseID);
    }

    /**
     * @description: 生成班级邀请码
     * @param: 班级id
     * @return:
     * @author jiwenlong
     * @date: 2024/2/26 21:57
     */
    @PutMapping("/gradeOfCode")
    public ResponseVO putGradeOfInvitationCode(@RequestParam("gradeID") Integer gradeID) {
        return gradeService.modifyGradeOfInvitationCode(gradeID);
    }

    @PostMapping("/grade/joinCourse")
    public ResponseVO postJoinCourse(@RequestParam("gradeId") Long gradeId, @RequestParam("courseId") Long courseId) {
        return gradeService.createGradeAndCourse(gradeId, courseId);
    }

    @PostMapping("/student/join")
    public ResponseVO postStudentAndClass(@RequestParam("type") String type, @RequestParam("text") String text, HttpServletRequest httpServletRequest) {
        ResponseVO responseVO = new ResponseVO();
        User user_state = (User) httpServletRequest.getSession().getAttribute("user_state");
        if (type.equals("class")) {
            responseVO = gradeService.createGradeAndUser(text, user_state);
        } else if (type.equals("course")) {
            responseVO =  courseService.createCourseAndUser(text,user_state);
        }
        return responseVO;
    }

    @GetMapping("/student/class")
    public ResponseVO getClassListByUser(@RequestParam("id") Long userId) throws IOException {
        if (userId == null) {
            return new ResponseVO(StatusCode.FAIL_SELECT.getCode(), StatusCode.FAIL_SELECT.getMessage());
        }
        return gradeService.queryClassByUser(userId);
    }


}
